import { defineStore } from 'pinia'
import type { UserState } from './types/user'
import { constantRoute } from '@/router/routes'
import { reqLogin, register } from '@/apis/user'
import { loginForm } from '@/apis/user/type'
import { SET_TOKEN, GET_TOKEN, REMOVE_TOKEN } from '@/utils/time'
const useUserStore = defineStore('User', {
  state: (): UserState => {
    return {
      menuRoutes: constantRoute,
      token: GET_TOKEN(),
    }
  },
  actions: {
    async userLogin(data: loginForm) {
      const result = await reqLogin(data)
      if (result.code == 0) {
        this.token = result.data
        //本地持久化存储
        SET_TOKEN(result.data)
        //返回一个成功的permise对象
        return 'ok'
      } else {
        return Promise.reject(new Error(result.data))
      }
    },
    async userRegister(data: loginForm) {
      const result = await register(data)
      console.log(result)

      if (result.code == 0) {
        return 'ok'
      } else {
        return Promise.reject(new Error(result.data))
      }
    },
  },
  getters: {},
})
export default useUserStore
